package siar.web.managedbeans;

import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import siar.webservice.BebidaBean;
import siar.webservice.EnderecoBean;
import siar.webservice.PessoaBean;
import siar.webservice.PratoBean;
import siar.webservice.ServicosSIARSoap;
import siar.webservice.VendaBean;

public class UsuarioMB {
	private String login;
	private String senha;
	private PessoaBean usuarioLogado;
	private List<PratoBean> pratosDoUsuario;
	private List<BebidaBean> bebidasDoUsuario;
	private String dataHoraVendaAtual; 
	private List<VendaBean> vendas;
	private EnderecoBean enderecoUsuario;
	private ExternalContext externalContext;
	private ServicosSIARSoap ws;
	
	public String efetuarLogin() {
		try {
			this.ws = (ServicosSIARSoap) Naming.lookup("rmi://localhost/servicosSIAR");
			PessoaBean usuario = ws.efetuarLoginWeb(this.login, this.senha);
			if (usuario != null){
				this.externalContext = FacesContext.getCurrentInstance().getExternalContext();
				HttpSession session = (HttpSession) externalContext.getSession(false);
				session.setAttribute("usuario", usuario);
				this.usuarioLogado = usuario;
				return "logou";
			} else {
				return "naoLogou";
			}
			/*
			ProgrammaticLogin progLogin = new ProgrammaticLogin();
			if (progLogin.login(this.login, this.senha, "siar", true)) {
				return "sucesso";
			} else {
				return "falha";
			}*/
		} catch (Exception e) {
			e.printStackTrace();
			return "falhaLogin";
		}
	}

	public String efetuarLogout(){
		FacesContext context = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) this.externalContext.getSession(false);
		session.invalidate();
		HttpServletResponse res = (HttpServletResponse) this.externalContext.getResponse();
		HttpServletRequest req = (HttpServletRequest) this.externalContext.getRequest();
		try {
			res.sendRedirect(req.getContextPath() + "/index.jsf");
		} catch (IOException e) {
			e.printStackTrace();
		}
		context.responseComplete();
		return null;
	}
	
	public List<PratoBean> getPratosDoUsuario() {
		PratoBean[] arrayP;
		try {
			arrayP = ws.pratosListarDaVendaAtual(this.usuarioLogado.getId());
			this.pratosDoUsuario= new ArrayList<PratoBean>();
			for (int i=0; i<arrayP.length; i++){
				this.pratosDoUsuario.add(arrayP[i]);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return this.pratosDoUsuario;
	}

	public List<BebidaBean> getBebidasDoUsuario() {
		BebidaBean[] arrayB;
		try {
			arrayB = ws.bebidasListarDaVendaAtual(usuarioLogado.getId());
			this.bebidasDoUsuario = new ArrayList<BebidaBean>();
			for (int i=0; i<arrayB.length; i++){
				this.bebidasDoUsuario.add(arrayB[i]);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bebidasDoUsuario;
	}
	
	public List<VendaBean> getVendas() {
		try {
			this.vendas = new ArrayList<VendaBean>();
			VendaBean v = ws.vendaBuscarPorCliente(usuarioLogado.getId());
			this.vendas.add(v);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return this.vendas;
	}
	
	public String getDataHoraVendaAtual() {
		List<VendaBean> vendas = getVendas();
		Calendar c = vendas.get(0).getHoraA();
		this.dataHoraVendaAtual = c.get(Calendar.DAY_OF_MONTH) + "/" + c.get(Calendar.MONTH) + "/" + c.get(Calendar.YEAR) + " à(s) " + c.get(Calendar.HOUR_OF_DAY) + "h" + c.get(Calendar.MINUTE) + "min"; 
		return this.dataHoraVendaAtual; 
	}
	
	public PessoaBean getUsuarioLogado(){
		return this.usuarioLogado;
	}
	
	public String verificarLogon(){
		if(getUsuarioLogado() != null){
			System.out.println(usuarioLogado.getNome());
			return "ok";
		}
		else
		 return "erro";
	}
	
	public EnderecoBean getEnderecoUsuario(){
		EnderecoBean endereco = null;
		try {
			this.ws = (ServicosSIARSoap) Naming.lookup("rmi://localhost/servicosSIAR");
			this.enderecoUsuario = ws.enderecoBuscar(usuarioLogado.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return this.enderecoUsuario;
	}
	
	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getSenha() {
		return senha;
	}

	public void setSenha(String senha) {
		this.senha = senha;
	}
}
